# 第 8 章 函数
# 8.1 定义函数
def greet_user1():
    print("你好")


greet_user1()


# 8.1.1 向函数传递信息
def greet_user2(name):
    print("你好," + name)


greet_user2("zzp")


# 8.1.2 实参和形参
# 8.2 传递实参
# 8.2.1 位置实参
def greet_user3(name, sex):
    print("姓名:" + name + " 性别:" + sex)


greet_user3("zzp", "男")
greet_user3("xm", "女")


# 8.2.2 关键字实参
def describe_pet(animal_type, pet_name):
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")


describe_pet(animal_type='hamster', pet_name='harry')


# 8.2.3 默认值
def describe_pet(pet_name, animal_type='dog'):
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")


describe_pet(pet_name='willie')


# 8.2.4 等效的函数调用
def desc_pet(pet_name, animal_type='dog'):
    print(pet_name + " " + animal_type)


desc_pet('while')
desc_pet(pet_name='while')
describe_pet('harry', 'hamster')
describe_pet(pet_name='harry', animal_type='hamster')
describe_pet(animal_type='hamster', pet_name='harry')


# 8.2.5 避免实参错误
# 8.3 返回值
# 8.3.1 返返回回简简单单值值
def getname(first_name, last_name):
    full_name = first_name + "  " + last_name
    return full_name.title()


name = getname("herman", "zzp")
print(name)


# 8.3.2 让实参变成可选的
def getname1(first_name, last_name=''):
    full_name = first_name + "  " + last_name
    return full_name.title()


name = getname1("herman", "zzp")
name = getname1("herman")
print(name)


# 8.3.3 返回字典
def getname2(first_name, last_name=''):
    persone = {'first': first_name, 'last': last_name}
    return persone


musan = getname2("herman", "zzp")
print(musan)


# 8.3.4 结合使用函数和while 循环
# def get_formatted_name(first_name, last_name):
#     full_name = first_name + ' ' + last_name
#     return full_name.title()
#
#
# while True:
#     print("\nPlease tell me your name:")
#     print("(enter 'q' at any time to quit)")
#     f_name = input("First name: ")
#     if f_name == 'q':
#         break
#     l_name = input("Last name: ")
#     if l_name == 'q':
#         break
#     formatted_name = get_formatted_name(f_name, l_name)
#     print("\nHello, " + formatted_name + "!")


# 8.4 传递列表
def greet_users(users):
    for name in users:
        print("hello, " + name)


usernames = ["zzp", "rgp"]
greet_users(usernames)


# 8.4.1 在函数中修改列表
def print_models(unprinted_designs, completed_models):
    while unprinted_designs:
        current_design = unprinted_designs.pop()
        print("Printing model: " + current_design)
        completed_models.append(current_design)


def show_completed_models(completed_models):
    print("\nThe following models have been printed:")
    for completed_model in completed_models:
        print(completed_model)


unprinted_designs = ['1', '2', '3']
completed_models = []
print_models(unprinted_designs, completed_models)
show_completed_models(unprinted_designs)
show_completed_models(completed_models)


# 8.4.2 禁止函数修改列表
# 切片表示法[:] 创建列表的副本
# print_models(unprinted_designs[:], completed_models)

# 8.5 传递任意数量的实参
def setname(*name):
    print(name)


setname("zzp")
setname("zzp", "herman")


# 8.5.1 结合使用位置实参和任意数量实参
def make_pizza(size, *types):
    print("大小:" + str(size))
    for top in types:
        print(top)


make_pizza(16)
make_pizza(16, "big")
make_pizza(16, "big", "longxia")


# 8.5.2 使用任意数量的关键字实参
def build_profile(first, last, **user_info):
    profile = {}
    profile['first_name'] = first
    profile['last_name'] = last
    for key, value in user_info.items():
        profile[key] = value
    return profile


user_profile = build_profile('albert', 'einstein', location='princeton', field='physics')
print(user_profile)
# 8.6 将函数存储在模块中
# 8.6.1 导入整个模块
